package com.yh.workbill;

import android.content.Context;

import com.yh.workbill.db.Company;
import com.yh.workbill.db.CompanyDao;
import com.yh.workbill.db.UnitCompany;
import com.yh.workbill.db.UnitCompanyDao;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

@Database(
        entities = {UserInfo.class, Company.class, UnitCompany.class},
        version = 2,
        exportSchema = false)
public abstract class AppDataBase extends RoomDatabase {

    private static AppDataBase INSTANCE;

    public static synchronized AppDataBase getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = Room.databaseBuilder(
                    context,
                    AppDataBase.class,
                    "workbill-data"
            ).addMigrations(Migration1_2)//刚好做一个room数据库字段补充升级的案例
                    .build();
        }
        return INSTANCE;
    }


    public abstract UserInfoDao getUserInfoDao();

    public abstract CompanyDao getCompanyDao();

    public abstract UnitCompanyDao getUnitCompanyDao();

    static final Migration Migration1_2 = new Migration(1, 2) {

        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //userinfo 对象中增加token
            database.execSQL("ALTER TABLE t_userinfo ADD COLUMN token TEXT NOT NULL");
        }
    };
}
